package com.lv.controller;

import com.alibaba.fastjson.JSONObject;
import com.lv.data.Result;
import com.lv.pojo.TbSpecification;
import com.lv.pojo.TbSpecificationOption;
import com.lv.service.SpecService;
import com.lv.util.Page;
import com.lv.vo.SpecVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("spec")
public class SpecController {


    @DubboReference
    public SpecService ss;

    @RequestMapping("tospec")
    public String tospec(){
        return "admin/specification";
    }

    @RequestMapping("getInfo")
    @ResponseBody
    public Result getInfo(@RequestParam(defaultValue = "1")Integer pageNum,
                          @RequestParam(defaultValue = "300") Integer pageSize){
        Page<TbSpecification> pb = ss.getInfo(pageNum,pageSize);
        return Result.OK().setData(pb);
    }

    //增加
    @RequestMapping("add")
    @ResponseBody
    public Integer add(TbSpecification spec,String arrStr){
            List<TbSpecificationOption> optionList = JSONObject.parseArray(arrStr, TbSpecificationOption.class);


        return ss.addSpecAndOption(spec,optionList);
    }

    //回显
    @RequestMapping("toupdate/{id}")
    @ResponseBody
    public SpecVO toupdate(@PathVariable("id") Long id){
        return ss.toupdate(id);
    }

    //修改
    @RequestMapping("update")
    @ResponseBody
    public Integer update(SpecVO specVO,String arrStr){
        List<TbSpecificationOption> optionList = JSONObject.parseArray(arrStr, TbSpecificationOption.class);
        specVO.setSpecOptionsList(optionList);
        return ss.update(specVO);
    }
    //删除
    @RequestMapping("delBrands")
    @ResponseBody
    public Integer delBrands(@RequestParam(value = "ids") List<Long> ids){

           Integer i =  ss.delBrands(ids);
           return i;
    }

    //查询全部
    @RequestMapping("selectAll")
    @ResponseBody
    public List<TbSpecification> selectAll(){
        return ss.selectAll();
    }
}
